//
//  AppDelegate.m
//  YiLin
//
//  Created by Zyark on 2018/5/24.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "AppDelegate.h"
#import "ZYTabBarController.h"
#import "ZYLoginMainVC.h"
#import "ZYNavigationController.h"
#import "WXApi.h"
#import "ZYWeChatConfig.h"
#import "ZYWeChatLoginViewModel.h"
#import "ZYGesturePasswordVerifyVC.h"
#import "ZYGesturePasswordVerifyVC.h"
#import <Bugly/Bugly.h>

@interface AppDelegate ()<WXApiDelegate>
@property(nonatomic,strong) ZYWeChatLoginViewModel *viewModel;
@property(nonatomic,strong) ZYGesturePasswordVerifyVC *vc;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if([ZYConfig isLogin]){
        [ZYTabBarController sharedInstance].selectedIndex = 0;
        self.window.rootViewController = [ZYTabBarController sharedInstance];
    }else{
        ZYLoginMainVC *loginVC = [[ZYLoginMainVC alloc] init];
        ZYNavigationController *nav = [[ZYNavigationController alloc] initWithRootViewController:loginVC];
        self.window.rootViewController = nav;
    }
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [WXApi registerApp:WX_APP_ID];
    [Bugly startWithAppId:@"cbdb971bee"];
    _viewModel = [[ZYWeChatLoginViewModel alloc] init];
    _vc = [[ZYGesturePasswordVerifyVC alloc] init];
    _vc.hidesBottomBarWhenPushed = YES;
    return YES;
}

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host {
    return YES;
}


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return  [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [WXApi handleOpenURL:url delegate:self];
}

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    return [WXApi handleOpenURL:url delegate:self];
}

- (void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[SendAuthResp class]]) {
        [self managerDidRecvAuthResponse:((SendAuthResp *)resp)];
    }
}

//微信授权登录
- (void)managerDidRecvAuthResponse:(SendAuthResp *)response{
    _viewModel.code = response.code;
    @weakify(self);
    [[_viewModel.loginCommand execute:nil] subscribeNext:^(id x) {
        @strongify(self);
        [[ZYTabBarController sharedInstance] setUp];
        [ZYTabBarController sharedInstance].selectedIndex = 0;
        self.window.rootViewController = [ZYTabBarController sharedInstance];
    } error:^(NSError *error) {
        [[ToastHelper sharedToastHelper] toastError:[NSErrorHelper handleErrorMessage:error]];
    }];
    [[[_viewModel.loginCommand executing] skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"applicationWillEnterForeground");
    if([ZYConfig isOnGesturePassword]){
        UITabBarController *tab = (UITabBarController *)_window.rootViewController;
        UINavigationController *nav = tab.viewControllers[tab.selectedIndex];
        if(![nav.viewControllers containsObject:_vc]){
            [nav pushViewController:_vc animated:YES];
        }
        
//        ZYNavigationController *nav = [[ZYNavigationController alloc] initWithRootViewController:vc];
//        [self.window.rootViewController.navigationController pushViewController:nav animated:YES];
    }
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    return UIInterfaceOrientationMaskPortrait;
}

@end
